package com.wiener.web.managebeans;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

@ManagedBean(name = "userManagedBean")
@SessionScoped
public class UserManagedBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = Logger.getLogger(UserManagedBean.class);

	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(final String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	public String cerrarSesion() {
		LOGGER.info("cerrarSesion");
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
		if (session != null)
			session.invalidate();

		return "login?faces-redirect=true";
	}

	public boolean getUsuarioLogueado() {
		return getUsername() != null;
	}

	public String loginGeneral() throws Exception {
		String page = "registroAlumno";
		try {
			LOGGER.info("usuario " + getUsername());
			return page;
		} catch (Exception e) {
			FacesContext context = FacesContext.getCurrentInstance();
			LOGGER.error("Error Login General", e);
			context.addMessage("messaje", new FacesMessage("Ha ocurrido un error. Por favor, vuelva a intentarlo en unos minutos."));
			return "error";
		}

	}

}
